package com.example.design.ddd.practice.domain.empmng.entity;
// imports ...


import com.example.design.ddd.practice.common.framework.domain.AuditableEntity;
import com.example.design.ddd.practice.common.framework.exception.BusinessException;

import java.time.LocalDateTime;

import static com.example.design.ddd.practice.domain.empmng.entity.EmpStatus.*;

public class Emp extends AuditableEntity {
    // other fields ...
    private EmpStatus status;

    public Emp(LocalDateTime createdAt, Long createdBy, EmpStatus status) {
        super(createdAt, createdBy);
        this.status = status;
    }

    // other getters and setters ...

    public EmpStatus getStatus() {
        return status;
    }

    //转正
    void becomeRegular() {
        // 调用业务规则: 试用期的员工才能被转正
        onlyProbationCanBecomeRegular(); 
        status = REGULAR;
    }

    //终止
    void terminate() {
        // 调用业务规则: 已经终止的员工不能再次终止
        shouldNotTerminateAgain();     
        status = TERMINATED;
    }

    // 实现业务规则
    private void onlyProbationCanBecomeRegular() {
        if (status != PROBATION) {
            throw new BusinessException("试用期员工才能转正！");
        }
    }

    private void shouldNotTerminateAgain() {
        if (status == TERMINATED) {
            throw new BusinessException("已经终止的员工不能再次终止！");
        }
    }
}